<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="auth" uri="http://www.recharge.com/authority" %>
<auth:authority role="0" />
<%--
这边要注意一个easyui 的问题，当panel tabs 这种href 引用页面
页面中不能有html body head 因为他只会把body 里面的内容 append 到页面中
--%>
<div id="productSup-dataCondition" class="easyui-panel"
     style="width:1095px;height:150px;padding:10px;background:#fafafa;"
     data-options="closable:false,
                collapsible:false,minimizable:false,maximizable:false">
    <form id="productSup-mainForm" method="post">

        <table>
            <tr>
                <td><label for="productId">商品编号:</label></td>
                <td><input class="easyui-validatebox" type="text" name="productId" id="productId"/></td>
                <td><label for="productName">商品名称:</label></td>
                <td><input class="easyui-validatebox" type="text" name="productName" id="productName"/></td>
                <td><label for="supId">供货商编号:</label></td>
                <td><input class="easyui-validatebox" type="text" name="supId" id="supId"/></td>
                <td><label for="supName">供货商名称:</label></td>
                <td><input class="easyui-validatebox" type="text" name="supName" id="supName"/></td>
                <td><label for="status">状态:</label></td>
                <td><select name="status" id="status">
                    <option selected></option>
                    <option value="true">打开</option>
                    <option value="false">关闭</option>
                </select></td>
            </tr>

            <tr>
                <td colspan="3"></td>
                <td><input type="button" id="submit_mainForm" value="查询" onclick="submitProductSupMainForm();">
                </td>
            </tr>
        </table>
    </form>
</div>


<div id="dataView" class="easyui-panel" title="查询结果"
     style="width:1095px;background:#fafafa; height: 415px;overflow-y:hidden"
     data-options="closable:false,
                collapsible:false,minimizable:false,maximizable:false">
    <table id="productSup-dg">
    </table>
</div>

<div id="productSup-selectProduct-dialog"></div>
<div id="productSup-selectSup-dialog"></div>

<script language="JavaScript" type="text/javascript">

    $().ready(function () {

        //初始化查询结果表格
        $('#productSup-dg').datagrid({
            columns:[[
                {field:'productId',title:'商品编号',width:100},
                {field:'productName',title:'商品名称',width:150},
                {field:'supId',title:'供货商编号',width:100},
                {field:'supName',title:'供货商名称',width:100},
                {field:'priority',title:'优先级',align:'center',width:100,formatter: function(value,row,index){
                    return '<a href="#" onclick="updateProductSupInfo(\''+row.productId+'\',\''+row.supId+'\',\''+row.priority+'\',null,null);return false;"><font color="#7fff00">'+value+'</font></a>';
                }},
                {field:'weight',title:'权重',align:'center',width:100,formatter: function(value,row,index){
                    return '<a href="#" onclick="updateProductSupInfo(\''+row.productId+'\',\''+row.supId+'\',null,\''+row.weight+'\',null);return false;"><font color="#7fff00">'+value+'</font></a>';
                }},
                {field:'status',title:'状态',align:'center',width:100,formatter: function(value,row,index){
                    if(value==true){
                        return '<a href="#" onclick="updateProductSupInfo(\''+row.productId+'\',\''+row.supId+'\',null,null,false);return false;"><font color="#7fff00">打开</font></a>';
                    }else{
                        return '<a href="#" onclick="updateProductSupInfo(\''+row.productId+'\',\''+row.supId+'\',null,null,true);return false;"><font color="#dc143c">关闭</font></a>';
                    }
                }}
            ]],
            pagination: true,
            height: 390,
            toolbar: [{
                iconCls: 'icon-add',
                text : "添加供货关系",
                handler: function(){
                    $('#productSup-selectProduct-dialog').dialog({
                        title: '添加供货商',
                        width: 700,
                        height: 500,
                        closed: false,
                        cache: false,
                        href : "productSup/selectProduct.jsp",
                        modal: true
                    });
                }
            }]
        });

        var pager = $('#productSup-dg').datagrid().datagrid('getPager');
        pager.pagination({
            onBeforeRefresh:function(pageNumber, pageSize){
                getProductSupListData(pageNumber,pageSize);
            },
            onSelectPage:function(pageNumber, pageSize){
                getProductSupListData(pageNumber,pageSize);
            }
        });


    });

    function getProductSupListData(pageNumber, pageSize){
        var param = $("#productSup-mainForm").serializeArray();
        param.push({name:"curr",value:pageNumber});
        param.push({name:"pageSize",value:pageSize});

        $.post("productSup/queryList", param,function(responseInfo){
            $('#productSup-dg').datagrid('loadData', {
                "total":responseInfo.count,
                "rows":responseInfo.data
            });
        });

    }

    function updateProductSupInfo(productId,supId,priority,weight,enable){

        if(enable==null){

            if(weight!=null){
                $.messager.prompt('Confirm','输入你要修改后的值',function(r){
                    var param = [];
                    param.push({name:"productId",value:productId});
                    param.push({name:"supId",value:supId});
                    param.push({name:"priority",value:priority});
                    param.push({name:"weight",value:r});
                    param.push({name:"status",value:enable});

                    $.post("productSup/updateInfo", param,function(responseInfo){
                    });
                });

                return ;
            }


            if(priority!=null){
                $.messager.prompt('Confirm','输入你要修改后的值',function(r){
                    var param = [];
                    param.push({name:"productId",value:productId});
                    param.push({name:"supId",value:supId});
                    param.push({name:"priority",value:r});
                    param.push({name:"weight",value:weight});
                    param.push({name:"status",value:enable});

                    $.post("productSup/updateInfo", param,function(responseInfo){
                    });
                });

                return ;
            }
        }

        var param = [];
        param.push({name:"productId",value:productId});
        param.push({name:"supId",value:supId});
        param.push({name:"priority",value:priority});
        param.push({name:"weight",value:weight});
        param.push({name:"status",value:enable});

        $.post("productSup/updateInfo", param,function(responseInfo){
        });
    }

    function submitProductSupMainForm() {
        var param = $("#productSup-mainForm").serializeArray();
        param.push({name:"curr",value:1});
        param.push({name:"pageSize",value:10});

        $.post("productSup/queryList", param,function(responseInfo){
            $('#productSup-dg').datagrid('loadData', {
                "total":responseInfo.count,
                "rows":responseInfo.data
            });
        });
    }
</script>